﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Windows.Forms;
using UCL;
using UCL.Forms;

namespace SoIM
{
    public partial class ucDepartmentList : UserControl
    {
        public TreeView TreeViewControl { get { return this.tvList; } }
        public bool ShowChildUser { get; set; }

        ImageList imglst;
        public ucDepartmentList()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            imglst = new ImageList();
            imglst.ColorDepth = ColorDepth.Depth24Bit;
            imglst.ImageSize = new Size(30, 30);
        }
        public void uc_Init(bool showUser = true, bool checkBoxes = false)
        {

            tvList.ShowLines = false;
            tvList.ItemHeight = 36;

            this.ShowChildUser = showUser;
            if (checkBoxes)
            {
                tvList.CheckBoxes = true;
                tvList.OnCheckedCheckingTreeNodes();
            }
            this.FormLoad();
            if (ShowChildUser) tvList.CollapseByLevel(4);
        }
        public void FormLoad()
        {
            // 清空内容
            tvList.Nodes.Clear();

            // 读取数据
            string xml = UCL.ADF.ADFS.Instance.Site.GetFileText("UserDB.soxf");
            if (xml.Length == 0) return;

            LoadImages(xml);

            // 加载数据
            tvList.LoadXmlString(xml, "所有部门", Guid.Empty.ToString());

            // 展开所有节点
            tvList.ExpandAll();
        }

        /// <summary>
        /// 加载图片
        /// </summary>
        /// <param name="xml"></param>
        /// <remarks>2013-10-29 by sutroon</remarks>
        private void LoadImages(string xml)
        {
            imglst.Images.Clear();
            string nodetag = "node", keytag = "ID", facetag = "Face";
            XElement xe = XElement.Parse(xml);
            LoadImages_Func(xe, imglst, nodetag, facetag, keytag);
            tvList.ImageList = imglst;
            tvList.ImageKey = keytag;
        }
        private void LoadImages_Func(XElement xe, ImageList imglst, string nodetag, string facetag, string keytag)
        {
            if (!xe.HasElements) return;
            string imgpath;
            foreach (XElement e in xe.Elements(nodetag))
            {
                if (e.Attribute(facetag) != null && e.Attribute(facetag).Value.Length > 0)
                {
                    imgpath = e.Attribute(facetag).GetValue();
                    if (imgpath.Length == 0) continue;
                    imgpath = UCL.ADF.ADFS.Instance.Site.GetFolder("Faces").ToString() + "\\" + Path.GetFileName(imgpath);
                    if (File.Exists(imgpath)) imglst.Images.Add(e.Attribute(keytag).GetValue(), Image.FromFile(imgpath));
                }
                if (e.HasElements) LoadImages_Func(e, imglst, nodetag, facetag, keytag);
            }
        }
    }
}
